package com.rogo.rogobox.models
{
	import com.rogo.mvc.sub.proxy.Proxy;
	import com.rogo.rogobox.AppFacade;
	import com.sina.microblog.data.MicroBlogUser;
	import com.sina.microblog.data.MicroBlogUsersRelationship;
	import com.sina.microblog.events.MicroBlogErrorEvent;
	import com.sina.microblog.events.MicroBlogEvent;
	
	/**
	 * 1.4 关着接口
	 * 
	 * @see http://open.weibo.com/wiki/API文档
	 * 
	 * @author rogo
	 */	
	public class FriendshipsProxy extends Proxy
	{
		public function FriendshipsProxy()
		{
			super(NAME);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Constants
		//
		//--------------------------------------------------------------------------
		
		public static const NAME:String = 'friendshipsProxy';
		
		//--------------------------------------------------------------------------
		//
		//  Varaibles
		//
		//--------------------------------------------------------------------------
		
		private var blog:BlogDelegate;
		
		//--------------------------------------------------------------------------
		//
		//  Overridden Methods
		//
		//--------------------------------------------------------------------------
		
		override public function onRegister():void
		{
			blog = BlogDelegate.getInstance();
			blog.addEventListener(MicroBlogErrorEvent.CHECK_IS_FOLLOWING_ERROR, blog_checkIsFollowingErrorHandler);
			blog.addEventListener(MicroBlogEvent.CHECK_IS_FOLLOWING_RESULT, blog_checkIsFollwoingResultHandler);
			blog.addEventListener(MicroBlogErrorEvent.FOLLOW_ERROR, blog_followErrorHandler);
			blog.addEventListener(MicroBlogEvent.FOLLOW_RESULT, blog_followResultHandler);
			blog.addEventListener(MicroBlogErrorEvent.CANCEL_FOLLOWING_ERROR, blog_cancelFollowingErrorHandler);
			blog.addEventListener(MicroBlogEvent.CANCEL_FOLLOWING_RESULT, blog_cancelFollowingResultHandler);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		/**
		 * 获取用户关系的详细情况
		 * 
		 * @param targetID 对方ID
		 */		
	    public function checkFollowing(targetID:String):void
		{
			blog.checkIsFollowing(targetID);
		}
		
		public function createFollowing(targetID:String):void
		{
			blog.follow(targetID);
		}
		
		public function cancelFollowing(targetID:String):void
		{
			blog.cancelFollowing(targetID);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Event Handler
		//
		//--------------------------------------------------------------------------
		
		private function blog_checkIsFollwoingResultHandler(e:MicroBlogEvent):void
		{
			var usr:MicroBlogUsersRelationship = e.result as MicroBlogUsersRelationship;
			if (!usr)
				return;
			
			sendNotification(AppFacade.CHECK_FOLLOWING_RESULT, usr);
		}
		
		private function blog_checkIsFollowingErrorHandler(e:MicroBlogErrorEvent):void
		{}
		
		private function blog_followErrorHandler(e:MicroBlogErrorEvent):void
		{
			sendNotification(AppFacade.CREATE_FOLLOWING_ERROR);
			sendNotification(AppFacade.SHOW_TIP, '关注失败');
		}
		
		private function blog_followResultHandler(e:MicroBlogEvent):void
		{
			var user:MicroBlogUser = e.result as MicroBlogUser;
			if (!user)
				return;
			
			sendNotification(AppFacade.CREATE_FOLLOWING_RESULT, user);
			sendNotification(AppFacade.SHOW_TIP, '关注成功');
		}
		
		private function blog_cancelFollowingErrorHandler(e:MicroBlogErrorEvent):void
		{}
		
		private function blog_cancelFollowingResultHandler(e:MicroBlogEvent):void
		{
			var user:MicroBlogUser = e.result as MicroBlogUser;
			if (!user)
				return;
			
			sendNotification(AppFacade.CANCEL_FOLLOWING_RESULT, user);
		}
		
	}
}